import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:package_info/package_info.dart';
import 'package:provide/provide.dart';
import 'package:qgchat/common/action/index.dart';
import 'package:qgchat/common/global/global.dart';
import 'package:qgchat/http/apis/video.dart';
import 'package:qgchat/http/io.dart';
import 'package:qgchat/page/my/my/widget/Tab/UserTab.dart';
import 'package:qgchat/states/states_model/socket.dart';
import 'package:qgchat/states/states_model/user.dart';
import 'package:url_launcher/url_launcher.dart';

class UserTabList extends StatelessWidget {
  const UserTabList({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final user = Provide.value<UserState>(context);
    final info = user.info;
    final isAnchor = (info?.memberType ?? 0) == 1;

    return Container(
      width: w100(context),
      margin: EdgeInsets.only(top: px(50)),
      child: Column(
        children: <Widget>[
          UserTab(
            title: '基本资料',
            iconPath: 'assets/images/icon/basicInformation.png',
            onPressed: () {
              goPage(context, '/basicInfo');
            },
          ),
          Provide<UserState>(
            builder: (context, child, user) {
              return UserTab(
                title: '我的视频',
                iconPath: 'assets/images/icon/myvideo.png',
                onPressed: () {
                  if (isAnchor) {
                    goPage(context, '/release');
                  } else {
                    goPage(context, '/myvideo');
                  }
                },
              );
            },
          ),
          UserTab(
            title: '我的钱包',
            iconPath: 'assets/images/icon/myPurse.png',
            onPressed: () {
              goPage(context, '/myWallet');
            },
          ),
          _BigLine(),
          isAnchor
              ? UserTab(
                  title: '认证信息',
                  iconPath: 'assets/images/icon/attestation.png',
                  onPressed: () {
                    goPage(context, '/attestation');
                  },
                )
              : UserTab(
                  title: '主播认证',
                  iconPath: 'assets/images/icon/attestation.png',
                  onPressed: () {
                    goPage(context, '/anchorAuth');
                  },
                ),
          isAnchor
              ? UserTab(
                  title: '我的微信',
                  iconPath: 'assets/images/icon/wx.png',
                  onPressed: () {
                    goPage(context, '/myWechat');
                  },
                )
              : SizedBox.shrink(),
          isAnchor
              ? UserTab(
                  title: '时光价值',
                  iconPath: 'assets/images/icon/timeValue.png',
                  onPressed: () {
                    goPage(context, '/timeValue');
                  },
                )
              : SizedBox.shrink(),
          isAnchor
              ? UserTab(
                  title: '我的动态',
                  iconPath: 'assets/images/icon/myDynamic.png',
                  onPressed: () {
                    goPage(context, '/myDynamic');
                  },
                )
              : SizedBox.shrink(),
          isAnchor
              ? UserTab(
                  title: '黑名单',
                  iconPath: 'assets/images/icon/blacklist.png',
                  onPressed: () {
                    goPage(
                      context,
                      '/blacklist',
                      param: {
                        'aid': state(context, 'user').info.id,
                      },
                    );
                  },
                )
              : SizedBox.shrink(),
          UserTab(
            title: '美颜设置',
            iconPath: 'assets/images/icon/meiyan.png',
            onPressed: () {
              goPage(context, '/face');
            },
          ),
          UserTab(
            title: '开启直播',
            iconPath: 'assets/images/icon/meiyan.png',
            onPressed: () {
              goPage(context, '/readyLive');
            },
          ),
          _BigLine(),
          UserTab(
            title: '邀请好友',
            iconPath: 'assets/images/icon/invitation.png',
            onPressed: () {
              goPage(context, '/inviteFriends');
            },
          ),
          UserTab(
            title: '在线客服',
            iconPath: 'assets/images/icon/serviceOnline.png',
            onPressed: () {
              final token = Config.CUSTOMERSERVICESTAFF;
              final socketState = Provide.value<SocketState>(context);
              VideoApis.tokenuserInfo(token).then((targetInfo) {
                return socketState.goChatPage(
                  context,
                  id: targetInfo.id,
                  token: targetInfo.memberToken,
                  name: targetInfo.memberNickname,
                  head: targetInfo.memberHead,
                );
              });
            },
          ),
          _BigLine(),
          UserTab(
            title: '隐私政策',
            iconPath: 'assets/images/icon/yinsi.png',
            onPressed: () async {
              String link = "http://www.tangzhijiaowh.cn/privacy.html";
              if (await canLaunch(link)) {
                await launch(link);
              }
            },
          ),
          UserTab(
            title: '用户协议',
            iconPath: 'assets/images/icon/yonghu.png',
            onPressed: () async {
              String link = "http://www.tangzhijiaowh.cn/user.html";
              if (await canLaunch(link)) {
                await launch(link);
              }
            },
          ),
          _BigLine(),
          _DontDisturb(),
          _SignOut(),
          _Copyright(),
        ],
      ),
    );
  }
}

class _BigLine extends StatelessWidget {
  const _BigLine({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: w100(context),
      height: px(16),
      color: col('f5f7fa'),
    );
  }
}

class _DontDisturb extends StatelessWidget {
  const _DontDisturb({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: px(690),
      height: px(120),
      color: Colors.white,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Container(
            child: Text(
              '勿扰模式',
              style: TextStyle(fontSize: px(34), color: col('303133')),
            ),
          ),
          Provide<UserState>(
            builder: (context, child, user) {
              if (user.info != null) {
                return CupertinoSwitch(
                  value: user.info.disturb == 1,
                  activeColor: col('6f53ff'),
                  onChanged: (bool b) {
                    if (b) {
                      Io.post('member/disturb', data: {'status': 1});
                    } else {
                      Io.post('member/disturb', data: {'status': 0});
                    }
                    user.disturb(b ? 1 : 0);
                  },
                );
              }

              return Container();
            },
          ),
        ],
      ),
    );
  }
}

class _SignOut extends StatelessWidget {
  const _SignOut({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(top: px(44)),
      color: col('f5f7fa'),
      child: Center(
        child: FlatButton(
          onPressed: () {
            CommonAction.clearAppData(context);
            goLogin();
          },
          child: Text(
            '退出登录',
            style: TextStyle(fontSize: px(34), color: col('fc3d39')),
          ),
        ),
      ),
    );
  }
}

class _Copyright extends StatelessWidget {
  const _Copyright({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: w100(context),
      height: px(250),
      color: col('f5f7fa'),
      child: Container(
        margin: EdgeInsets.only(top: px(87)),
        child: Column(
          children: <Widget>[
            Text(
              'Copyright ©2020-2020\n棠之娇网络科技版权所有',
              textAlign: TextAlign.center,
              style: TextStyle(
                fontSize: px(22),
                color: col('bfc2cc'),
              ),
            ),
            FutureBuilder(
              future: PackageInfo.fromPlatform(),
              initialData: null,
              builder: (BuildContext context, AsyncSnapshot snapshot) {
                final packageInfo = snapshot.data;
                return Padding(
                  padding: EdgeInsets.only(top: px(20)),
                  child: Text(
                    'V${packageInfo?.version ?? ''}',
                    textAlign: TextAlign.center,
                    style: TextStyle(
                      fontSize: px(30),
                    ),
                  ),
                );
              },
            ),
          ],
        ),
      ),
    );
  }
}
